package com.zhongkai.app;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.BigUnitBean;
import com.zhongkai.Bean.ErrorCodeBean;
import com.zhongkai.StringStatic;
import com.zhongkai.app.service.BigDataUnitServcie;
import com.zhongkai.jedis.JedisClient;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.io.IOException;

public class MyMessageListener implements MessageListener {

    @Autowired
    private BigDataUnitServcie bigDataUnitServcie;
    @Autowired
    private JedisClient jedisClient;
/*    @Autowired
    private RedisUtils redisUtils;*/



    @Override
    public void onMessage(Message message) {

        try {
            TextMessage textMessage = (TextMessage) message;
            //取消息内容
            String json = textMessage.getText();

            // System.out.println("2:::"+json);


            if (json.contains("BigUnitBean:")) {

                json = json.replace("BigUnitBean:", "");
                //  json = json.replace("\t  ", "");

                System.out.println(json);

                com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();

                BigUnitBean bigUnitBean = mapper.readValue(json, BigUnitBean.class);  //当前到来的最新数据

                //    String unit_json = mapper.writeValueAsString(bigUnitBean); //实时数据
                long millis = new DateTime().getMillis(); //获取当前系统时间戳

                bigUnitBean.setTime(millis); //消息到来时间

                String big_unit_uuid = bigUnitBean.getUnit_uuid(); //取出uuid


                if (!"0".equals(bigUnitBean.getWarning())) { //设备报警 直接存储

                    //更新设备数据
                    bigDataUnitServcie.updateUnit(
                            big_unit_uuid, //设备id
                            bigUnitBean.getState(), //状态
                            bigUnitBean.getPower_all(), //总电量
                            bigUnitBean.getWarning(), //是否报警
                            bigUnitBean.getUser_t()//设备当前温度
                          //  bigUnitBean.getIp() //设备ip
                    );


                    // 数据存入数据库

                    // 1设置存入时间

                    bigUnitBean.setInsertionTime(millis);//存入时间

                    jedisClient.set(StringStatic.REDIS_UNIT + big_unit_uuid, json); //缓存300秒数据
                    jedisClient.expire(StringStatic.REDIS_UNIT + big_unit_uuid, 300);//重新设置过期时间


                    // ErrorCodeBean.err_code(bigUnitBean.getWarning_code());

                    bigDataUnitServcie.addErrorData(
                            bigUnitBean.getState(),
                            bigUnitBean.getSignal(),
                            bigUnitBean.getPower(),
                            bigUnitBean.getForce(),
                            bigUnitBean.getVoltage_currente(),
                            bigUnitBean.getPower_all(),
                            bigUnitBean.getUser_t(),
                            millis,
                            bigUnitBean.getUser_water_r(),
                            bigUnitBean.getUser_water_s(),
                            bigUnitBean.getUser_stressr_r(),
                            bigUnitBean.getUser_stressr_s(),
                            bigUnitBean.getGround_water_r(),
                            bigUnitBean.getGround_water_s(),
                            bigUnitBean.getGround_stressr_r(),
                            bigUnitBean.getGround_stressr_s(),
                            bigUnitBean.getUnit_in(),
                            bigUnitBean.getUnit_out(),
                            bigUnitBean.getEnvironment(),
                            bigUnitBean.getSupply_pressure(),
                            big_unit_uuid,
                            bigUnitBean.getWarning(),
                            bigUnitBean.getWarning_code(),
                            ErrorCodeBean.err_code(bigUnitBean.getWarning_code()),//此处需要根据错误代码 存入对应错误原因

                            bigUnitBean.getSys_stop(),
                            bigUnitBean.getCompressor(),
                            bigUnitBean.getGroundpump(),
                            bigUnitBean.getSysopen_num(),
                            bigUnitBean.getCompreopen_num(),
                            bigUnitBean.getLocking()
                    );
                    return;
                } else { //正常数据

                    //先取出缓存

                    //判断是否为空

                    //不为空 把缓存数据取出 转成bean

                    //此时判断缓存的数据 InsertionTime是否为0

                    //为0则把当前时间存入

                    //再把类转json

                    //存入redis

                    //缓存的时间不是0

                    //把缓存的时间取出 存入本次

                    //直接对比

                    String value = (String) jedisClient.get(StringStatic.REDIS_UNIT + big_unit_uuid); // 取出缓存的旧数据

                    String error_list = jedisClient.get(StringStatic.ERROR_UNIT); //报警设备集合

                    if (!StringUtils.isNullOrEmpty(error_list)) { //集合不为空 而此时设备数据却为正常数据

                        //拆分为数据
                        String[] split = error_list.split(",");

                        for (String s : split) {//遍历
                            if (s.equals(big_unit_uuid)) { //当前数据 之前报过警 立即更新状态

                                bigUnitBean.setInsertionTime(millis);//存入数据库的时间

                                json = mapper.writeValueAsString(bigUnitBean); //转成新的josn

                                jedisClient.set(StringStatic.REDIS_UNIT + big_unit_uuid, json); //缓存300秒数据
                                jedisClient.expire(StringStatic.REDIS_UNIT + big_unit_uuid, 300);//重新设置过期时间
                                //   redisUtils.set(StringStatic.REDIS_UNIT + big_unit_uuid, json, 18); //缓存18秒数据


                                bigDataUnitServcie.updateUnit(
                                        big_unit_uuid, //设备id
                                        bigUnitBean.getState(), //状态
                                        bigUnitBean.getPower_all(), //总电量
                                        bigUnitBean.getWarning(), //是否报警
                                        bigUnitBean.getUser_t()//设备当前温度
                                        //  bigUnitBean.getIp() //设备ip
                                );
                                //存入正常数据
                                bigDataUnitServcie.addUnitData(
                                        bigUnitBean.getState(),
                                        bigUnitBean.getSignal(),
                                        bigUnitBean.getPower(),
                                        bigUnitBean.getForce(),
                                        bigUnitBean.getVoltage_currente(),
                                        bigUnitBean.getPower_all(),
                                        bigUnitBean.getUser_t(),
                                        millis,
                                        bigUnitBean.getUser_water_r(),
                                        bigUnitBean.getUser_water_s(),
                                        bigUnitBean.getUser_stressr_r(),
                                        bigUnitBean.getUser_stressr_s(),
                                        bigUnitBean.getGround_water_r(),
                                        bigUnitBean.getGround_water_s(),
                                        bigUnitBean.getGround_stressr_r(),
                                        bigUnitBean.getGround_stressr_s(),
                                        bigUnitBean.getUnit_in(),
                                        bigUnitBean.getUnit_out(),
                                        bigUnitBean.getEnvironment(),
                                        bigUnitBean.getSupply_pressure(),
                                        big_unit_uuid,
                                        bigUnitBean.getWarning(),
                                        bigUnitBean.getWarning_code(),

                                        bigUnitBean.getSys_stop(),
                                        bigUnitBean.getCompressor(),
                                        bigUnitBean.getGroundpump(),
                                        bigUnitBean.getSysopen_num(),
                                        bigUnitBean.getCompreopen_num(),
                                        bigUnitBean.getLocking());


                                error_list = error_list.replace(big_unit_uuid + ",", "");

                               // jedisClient.set(StringStatic.ERROR_UNIT, error_list); //报警设备集合
                                return;
                            }
                        }
                    }
                    // String toDate;

                    if (!StringUtils.isNullOrEmpty(value)) { //判断是否为空  不为空 取出数据

                        BigUnitBean unitBean = mapper.readValue(value, BigUnitBean.class);


                        if (unitBean.getInsertionTime() == 0l) { //如果存入时间为0


                            bigUnitBean.setInsertionTime(millis); //把当前当做上次存入的时间 OK

                            json = mapper.writeValueAsString(bigUnitBean);
                        } else {


                            bigUnitBean.setInsertionTime(unitBean.getInsertionTime()); //把上次存入的时间存入这次

                            json = mapper.writeValueAsString(bigUnitBean);

                        }


                        long time = millis - unitBean.getInsertionTime();
                        System.out.println("存入" + time);

                        if (time > 300000) { //大于5分钟 存入数据库
                            //更新设备数据

                            System.out.println("执行");
                            bigDataUnitServcie.updateUnit(
                                    big_unit_uuid, //设备id
                                    bigUnitBean.getState(), //状态
                                    bigUnitBean.getPower_all(), //总电量
                                    bigUnitBean.getWarning(), //是否报警
                                    bigUnitBean.getUser_t()//设备当前温度
                                    //  bigUnitBean.getIp() //设备ip
                            );


                            //存入正常数据
                            bigDataUnitServcie.addUnitData(
                                    bigUnitBean.getState(),
                                    bigUnitBean.getSignal(),
                                    bigUnitBean.getPower(),
                                    bigUnitBean.getForce(),
                                    bigUnitBean.getVoltage_currente(),
                                    bigUnitBean.getPower_all(),
                                    bigUnitBean.getUser_t(),
                                    millis,
                                    bigUnitBean.getUser_water_r(),
                                    bigUnitBean.getUser_water_s(),
                                    bigUnitBean.getUser_stressr_r(),
                                    bigUnitBean.getUser_stressr_s(),
                                    bigUnitBean.getGround_water_r(),
                                    bigUnitBean.getGround_water_s(),
                                    bigUnitBean.getGround_stressr_r(),
                                    bigUnitBean.getGround_stressr_s(),
                                    bigUnitBean.getUnit_in(),
                                    bigUnitBean.getUnit_out(),
                                    bigUnitBean.getEnvironment(),
                                    bigUnitBean.getSupply_pressure(),
                                    big_unit_uuid,
                                    bigUnitBean.getWarning(),
                                    bigUnitBean.getWarning_code(),

                                    bigUnitBean.getSys_stop(),
                                    bigUnitBean.getCompressor(),
                                    bigUnitBean.getGroundpump(),
                                    bigUnitBean.getSysopen_num(),
                                    bigUnitBean.getCompreopen_num(),
                                    bigUnitBean.getLocking()


                            );

                            // bigUnitBean.setTime();
                            bigUnitBean.setInsertionTime(millis);//存入数据库的时间

                            json = mapper.writeValueAsString(bigUnitBean); //转成新的josn

                            //redisUtils.set(StringStatic.REDIS_UNIT + big_unit_uuid, json, 18); //缓存180秒数据

                            jedisClient.set(StringStatic.REDIS_UNIT + big_unit_uuid, json); //缓存300秒数据
                            jedisClient.expire(StringStatic.REDIS_UNIT + big_unit_uuid, 300);//重新设置过期时间

                        } else {  //小于30分钟 存入缓存
                            //  redisUtils.set(StringStatic.REDIS_UNIT + big_unit_uuid, json, 18); //缓存180秒数据

                            jedisClient.set(StringStatic.REDIS_UNIT + big_unit_uuid, json); //缓存300秒数据
                            jedisClient.expire(StringStatic.REDIS_UNIT + big_unit_uuid, 300);//重新设置过期时间
                        }

                        return;
                    } else {                                             //缓存为空


                        // 1设置存入时间

                        bigUnitBean.setInsertionTime(millis);//存入数据库的时间

                        json = mapper.writeValueAsString(bigUnitBean); //转成新的josn

                        jedisClient.set(StringStatic.REDIS_UNIT + big_unit_uuid, json); //缓存300秒数据
                        jedisClient.expire(StringStatic.REDIS_UNIT + big_unit_uuid, 300);//重新设置过期时间
                        //   redisUtils.set(StringStatic.REDIS_UNIT + big_unit_uuid, json, 18); //缓存18秒数据


                        bigDataUnitServcie.updateUnit(
                                big_unit_uuid, //设备id
                                bigUnitBean.getState(), //状态
                                bigUnitBean.getPower_all(), //总电量
                                bigUnitBean.getWarning(), //是否报警
                                bigUnitBean.getUser_t()//设备当前温度
                                //  bigUnitBean.getIp() //设备ip
                        );
                        //存入正常数据
                        bigDataUnitServcie.addUnitData(
                                bigUnitBean.getState(),
                                bigUnitBean.getSignal(),
                                bigUnitBean.getPower(),
                                bigUnitBean.getForce(),
                                bigUnitBean.getVoltage_currente(),
                                bigUnitBean.getPower_all(),
                                bigUnitBean.getUser_t(),
                                millis,
                                bigUnitBean.getUser_water_r(),
                                bigUnitBean.getUser_water_s(),
                                bigUnitBean.getUser_stressr_r(),
                                bigUnitBean.getUser_stressr_s(),
                                bigUnitBean.getGround_water_r(),
                                bigUnitBean.getGround_water_s(),
                                bigUnitBean.getGround_stressr_r(),
                                bigUnitBean.getGround_stressr_s(),
                                bigUnitBean.getUnit_in(),
                                bigUnitBean.getUnit_out(),
                                bigUnitBean.getEnvironment(),
                                bigUnitBean.getSupply_pressure(),
                                big_unit_uuid,
                                bigUnitBean.getWarning(),
                                bigUnitBean.getWarning_code(),

                                bigUnitBean.getSys_stop(),
                                bigUnitBean.getCompressor(),
                                bigUnitBean.getGroundpump(),
                                bigUnitBean.getSysopen_num(),
                                bigUnitBean.getCompreopen_num(),
                                bigUnitBean.getLocking()

                        );

                        return;
                    }


                }


            }

        } catch (JMSException e) {
            e.printStackTrace();
            return;
        } catch (JsonParseException e) {
            e.printStackTrace();
            return;
        } catch (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException e) {
            e.printStackTrace();
            return;
        } catch (JsonMappingException e) {
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
}
